home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_11.lha / 8_11 / rc.h < prev    next >
C/C++ Source or Header  |  1993-08-08  |  3KB  |  135 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  6. * The C++ Answer Book */
  7. * Tony Hansen */
  8. * All rights reserved. */
  9. / Exercise 8.11
  10. / Generic random access to objects kept within a file.
  11. / The user would use the generic class randomOBJ
  12. / for a user type TYPE by the following steps:
  13. /
  14. / declare(randomOBJ,TYPE)
  15. / implement(randomOBJ,TYPE)
  16. / ...
  17. / randomOBJ(TYPE) x("filename",
  18. /             read-err-func, write-err-func);
  19. / randomOBJ(TYPE) y("filename");
  20.  
  21. ifndef RANDACCESS_H
  22. define RANDACCESS_H
  23. include <stdio.h>
  24. include <generic.h>
  25.  
  26. include "8_11a.h" /* EXPAND */
  27.  
  28. *
  29.   An extended FILE which keeps track of its
  30.   current location within the file. This class
  31.   has no type-specific implementation and can
  32.   be used across all instantiations of randomOBJ.
  33. /
  34.  
  35. lass eFILE
  36.  
  37.    long fileoffset;
  38.    FILE *fp;
  39.    eFILE() { };
  40.  
  41. ublic:
  42.    friend eFILE *efopen(char *file);
  43.    friend void efclose(eFILE *efp);
  44.    friend int egetobj(eFILE *efp, long offset,
  45. void *obj, unsigned szobj);
  46.    friend int eputobj(eFILE *efp, long offset,
  47. void *obj, unsigned szobj);
  48. ;
  49.  
  50. define randomOBJdeclare(type)                \
  51.    class randomOBJ(type);                \
  52.                         \
  53.    class OBJrandomloc(type)                \
  54.    {                            \
  55. randomOBJ(type) *rOBJ;                \
  56. long thisoffset;                \
  57.                         \
  58.    public:                        \
  59. OBJrandomloc(type)(randomOBJ(type) *robj,    \
  60.            long offset)            \
  61. {                        \
  62.     rOBJ = robj;                \
  63.     thisoffset = offset;            \
  64. }                        \
  65.                         \
  66. operator type();                \
  67.                         \
  68. OBJrandomloc(type) operator=(type newobj);    \
  69. OBJrandomloc(type) operator=(            \
  70.     OBJrandomloc(type) r);            \
  71.    };                            \
  72.                         \
  73.    class randomOBJ(type)                \
  74.    {                            \
  75. eFILE *rfp;                    \
  76. void (*rdrfnc)(type *);                \
  77. void (*wtrfnc)(type *);                \
  78. friend OBJrandomloc(type);            \
  79. OBJrandomloc(type) operator=(type newobj);    \
  80.                         \
  81.    public:                        \
  82. randomOBJ(type)(char *filename,            \
  83.     void (*rfnc)(type *) = 0,            \
  84.     void (*wfnc)(type *) = 0)            \
  85. {                        \
  86.     rfp = efopen(filename);            \
  87.     rdrfnc = rfnc;                \
  88.     wtrfnc = wfnc;                \
  89. }                        \
  90.                         \
  91. ~randomOBJ(type)()                \
  92. {                        \
  93.     efclose(rfp);                \
  94. }                        \
  95.                         \
  96. int openedokay()                \
  97. {                        \
  98.     return rfp != 0;                \
  99. }                        \
  100.                         \
  101. OBJrandomloc(type) operator[](long offset)    \
  102. {                        \
  103.     OBJrandomloc(type) ret(this, offset);    \
  104.     return ret;                    \
  105. }                        \
  106.    };
  107.  
  108. define randomOBJimplement(type)            \
  109.    OBJrandomloc(type)::operator type()            \
  110.    {                            \
  111. type x;                        \
  112. if (!egetobj(rOBJ->rfp, thisoffset, &x,        \
  113.          sizeof x))                \
  114.     if (rOBJ->rdrfnc)                \
  115.     (*rOBJ->rdrfnc)(&x);            \
  116. return x;                    \
  117.    }                            \
  118.                         \
  119.    OBJrandomloc(type) OBJrandomloc(type)::operator=(    \
  120. type newobj)                    \
  121.    {                            \
  122. if (!eputobj(rOBJ->rfp, thisoffset,        \
  123.          &newobj, sizeof newobj))        \
  124.     if (rOBJ->wtrfnc)                \
  125.     (*rOBJ->wtrfnc)(&newobj);        \
  126. return *this;                    \
  127.    }                            \
  128.                         \
  129.    OBJrandomloc(type) OBJrandomloc(type)::operator=(    \
  130.              OBJrandomloc(type) r)    \
  131.    {                            \
  132. return (*this = (type) r);            \
  133.    }
  134. endif /* RANDACCESS_H */
  135.